<?php

namespace app\admin\controller\product;

use app\common\controller\Backend;

class Type extends Backend
{
    protected $model = null;

    protected $relationSearch = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Product.Type');
    }

    // 查看
    public function index()
    {
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    //添加
    public function add()
    {
        if ($this->request->isPost())
        {
            // 获取表单数据
            $params = $this->request->post('row/a');

            // 加校验器
            $result = $this->model->save($params);

            if ($result === false)
            {
                $this->error($this->model->getError());
                exit;
            }

            $this->success();
        }

        return $this->view->fetch();
    }

    // 编辑
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);

        if (!$row)
        {
            $this->error(__('商品分类不存在'));
        }

        if ($this->request->isPost())
        {
            // 获取表单数据
            $params = $this->request->post('row/a');
            $params['id'] = $ids;

            $result = $this->model->isUpdate(true)->save($params);

            if ($result === false)
            {
                $this->error($this->model->getError());
                exit;
            }

            //判断是否有上传新图片
            if ($row['thumb'] != $params['thumb'])
            {
                //不相等就说明有换图片了，就删除掉旧图片
                @is_file("." . $row['thumb']) && @unlink("." . $row['thumb']);
            }

            $this->success();
        }

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

//     // 删除
//     public function del($ids = null)
//     {
//         $row = $this->model->select($ids);

//         if (!$row)
//         {
//             $this->error(__('课程不存在'));
//         }

//         $result = $this->model->destroy($ids);

//         if ($result === false)
//         {
//             $this->error($this->model->getError());
//             exit;
//         }

//         $this->success();
//     }

    
//     // 回收站列表
//     public function recyclebin()
//     {
//         if ($this->request->isAjax())
//         {
//             list($where, $sort, $order, $offset, $limit) = $this->buildparams();

//             $total = $this->model
//                 ->onlyTrashed()
//                 ->with(['category'])
//                 ->where($where)
//                 ->order($sort, $order)
//                 ->count();

//             $list = $this->model
//                 ->onlyTrashed()
//                 ->with(['category'])
//                 ->where($where)
//                 ->order($sort, $order)
//                 ->limit($offset, $limit)
//                 ->select();

//             $result = array("total" => $total, "rows" => $list);

//             return json($result);
//         }
//     }

//     // 还原
//     public function restore($ids = null)
//     {
//         $row = $this->model->onlyTrashed()->select($ids);

//         if (!$row)
//         {
//             $this->error(__('课程不存在'));
//         }

//         $result = $this->model->onlyTrashed()->where('id', 'IN', $ids)->update(['deletetime' => null]);

//         if ($result === false)
//         {
//             $this->error($this->model->getError());
//             exit;
//         }

//         $this->success();
//     }

//     // 销毁
//     public function destroy($ids = null)
//     {
//         $row = $this->model->onlyTrashed()->select($ids);

//         if (!$row)
//         {
//             $this->error(__('课程不存在'));
//         }

//         $urllist = $this->ChapterModel->where('subid', 'IN', $ids)->column('url');

//         $result = $this->model->destroy($ids,true);

//         if ($result === false)
//         {
//             $this->error($this->model->getError());
//             exit;
//         }

//         // 删除课程图片
//         foreach($row as $item)
//         {
//             @is_file("." . $item['thumbs']) && @unlink("." . $item['thumbs']);
//         }

//         // 删除课程章节视频
//         foreach($urllist as $item)
//         {
//             @is_file("." . $item) && @unlink("." . $item);
//         }

//         $this->success();

//     }
}
